<template>
	<view class="compose_deta">
		<Navbar>合成</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="banner">
				<image :src="cover"></image>
				<view class="info">
					<view class="name">{{name}}</view>
					<view class="number">剩余：{{number}}份</view>
				</view>
			</view>
			<view class="sub_title">
				<image :src="`${baseImageUrl}/home/xs.png`"></image>
				<text>请选择藏品</text>
			</view>
			<view class="list">
				<view class="item" v-for="item in dataList">
					<view>
						<image :src="cover" class="cover"></image>
						<view class="name">{{item.name}}</view>
					</view>
					<view @click="choiceClick(item.id)">
						<text class="add">添加</text>
						<image :src="`${baseImageUrl}/common/add2.png`" class="add_image"></image>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="footer">
			<view class="btn_box">
				<view class="number">已选{{total}}个</view>
				<view class="btn" @click="submit">立即合成</view>
			</view>
		</view>

		<u-popup :show="show" bgColor="transparent" mode="center" @close="close">
			<view class="compose_list">
				<view class="close" @click="show=false">
					<image :src="`${baseImageUrl}/common/close1.png`"></image>
				</view>
				<view class="title">
					<view>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
						<text>请选择原料藏品</text>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
					</view>
				</view>
				<scroll-view scroll-x class="scroll_box">
					<view class="item" v-for="item in popupList">
						<view class="item_cover" @click="changePopupIndex(item)">
							<image :src="item.product_cover" class="item_cover"></image>
							<image :src="`${baseImageUrl}/common/checked.png`" class="checkbox"
								v-if="popupIndexTotal.includes(item.id)"></image>
						</view>
					</view>
				</scroll-view>
				<view class="btn" @click="show=false">确定</view>
			</view>
		</u-popup>

		<u-popup :show="popup_show" bgColor="transparent" mode="center" @close="close">
			<ComposeResult :id="order_id" @close="close" />
		</u-popup>
	</view>
</template>

<script>
	import {
		composeInfo,
		orderLists,
		composeOperate
	} from '@/request/api.js';
	import ComposeList from '@/components/home/ComposeList.vue';
	import ComposeResult from '@/components/home/ComposeResult.vue';
	export default {
		components: {
			ComposeList,
			ComposeResult
		},
		data() {
			return {
				id: null,
				cover: "",
				name: "",
				number: "",
				dataList: [],

				popupList: [],
				popupIndex: [],
				popupIndexTotal: [],
				total: 0,

				show: false,

				order_id: null,
				popup_show: false
			}
		},
		async onLoad(option) {
			this.id = option.id;
			let {
				info
			} = await composeInfo({
				id: this.id
			});
			this.cover = info.cover;
			this.name = info.name;
			this.number = info.number;

			let need = info.need;
			let dataList = [];
			need.forEach(item => {
				dataList.push({
					id: item.product.id,
					cover: item.product.cover,
					name: item.product.name,
					total: item.number,
					number: 0,
					popupIndex: []
				})
			});
			this.dataList = dataList;
		},
		methods: {
			async choiceClick(id) {
				let {
					list
				} = await orderLists({
					page: 1,
					limit: 100,
					product_id: id,
					status: 1
				});
				this.popupList = list.list;
				this.show = true;
			},
			changePopupIndex(item) {
				let id = item.id;
				let pid = item.product_id;
				let dataList = this.dataList;
				let dataItem = dataList.find(item => item.id == pid);
				let total = dataItem.total;
				let popupIndex = dataItem.popupIndex;
				let idx = popupIndex.indexOf(id);
				idx > -1 ? popupIndex.splice(idx, 1) : popupIndex.push(id);
				this.popupIndex = popupIndex;
				if (popupIndex.length > total) {
					popupIndex.splice(popupIndex.length - 1, 1);
					this.$u.toast("选择上限");
				};
				dataItem.number = popupIndex.length;

				let popupIndexTotal = [];
				dataList.forEach(item => {
					popupIndexTotal = [...popupIndexTotal, ...item.popupIndex];
				});
				this.popupIndexTotal = popupIndexTotal;
				this.total = popupIndexTotal.length;
			},
			async submit() {
				let dataList = this.dataList;
				let popupIndex = [];
				dataList.forEach(item => {
					popupIndex = [...popupIndex, ...item.popupIndex];
				});
				let res = await composeOperate({
					id: this.id,
					order_ids: popupIndex
				});
				this.order_id = res.order_id;
				this.popup_show = true;
			},
			close() {
				this.popup_show = false;
			}
		}
	}
</script>

<style scoped lang="scss">
	$footerHeight: 158rpx;

	.compose_deta {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight} - #{$footerHeight});
			box-sizing: border-box;

			.banner {
				width: 100%;
				padding: 210rpx 30rpx 0;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/bg4.png) no-repeat;
				background-size: 100% 832rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				image {
					width: 500rpx;
					height: 500rpx;
				}

				.info {
					width: 100%;
					height: 180rpx;
					margin-top: 62rpx;
					padding: 40rpx 0;
					box-sizing: border-box;
					background: url(#{$baseImageUrl}/bg/690_180.png) no-repeat;
					background-size: 100% 100%;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					align-items: center;

					.name {
						color: #494338;
						font-size: 50rpx;
						font-weight: 400;
						font-family: LiSu;
					}

					.number {
						width: 246rpx;
						height: 44rpx;
						line-height: 44rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 28rpx;
						font-family: LiSu;
						font-weight: 400;
						background: url(#{$baseImageUrl}/common/label2.png) no-repeat;
						background-size: 100% 100%;
					}
				}
			}

			.sub_title {
				height: 98rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}

				text {
					color: rgba(255, 255, 255, .5);
					font-size: 32rpx;
					font-family: SimSun;
					font-weight: 400;
					padding-left: 12rpx;
					box-sizing: border-box;
				}
			}

			.list {
				padding: 0 30rpx;
				box-sizing: border-box;

				.item {
					width: 100%;
					height: 116rpx;
					margin-bottom: 10rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					background: url(#{$baseImageUrl}/common/item_690_116.png) no-repeat;
					background-size: 100% 100%;
					display: flex;
					justify-content: space-between;

					&>view {
						display: flex;
						align-items: center;
					}

					.cover {
						width: 80rpx;
						height: 80rpx;
					}

					.name {
						color: #494338;
						font-size: 36rpx;
						font-weight: 400;
						font-family: LiSu;
						margin-left: 20rpx;
					}

					.add {
						color: #494338;
						font-size: 28rpx;
						font-weight: 400;
						font-family: SimSun;
					}

					.add_image {
						width: 80rpx;
						height: 80rpx;
					}
				}
			}
		}

		.footer {
			width: 100%;
			height: $footerHeight;
			background: #29170D;
			border-top: 2px solid #BC8478;
			padding: 18rpx 40rpx;
			box-sizing: border-box;

			.btn_box {
				display: flex;
				justify-content: space-between;
				align-items: center;


				.number {
					color: #FFFFFF;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
				}

				.btn {
					width: 210rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 30rpx;
					font-weight: 400;
					font-family: SimSun;
					background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
					background-size: 100% 100%;

					&.not {
						background: url(#{$baseImageUrl}/common/btn2.png) no-repeat;
						background-size: 100% 100%;
					}
				}
			}
		}
	}

	.compose_list {
		width: 688rpx;
		height: 606rpx;
		padding: 55rpx 60rpx 70rpx;
		box-sizing: border-box;
		background: url(#{$baseImageUrl}/bg/688_606.png) no-repeat;
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		position: relative;

		.close {
			width: 122rpx;
			height: 122rpx;
			position: absolute;
			right: 0rpx;
			top: 0rpx;
			transform: translate(50rpx, -50rpx);
			z-index: 2;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			display: flex;
			justify-content: center;
			align-items: center;

			view {
				height: 30rpx;
				display: flex;
				align-items: center;

				image {
					width: 30rpx;
					height: 30rpx;
				}

				text {
					color: #494338;
					font-size: 42rpx;
					font-weight: 400;
					font-family: LiSu;
					margin: 0 10rpx;
				}
			}
		}

		.scroll_box {
			white-space: nowrap;

			.item {
				display: inline-block;
				width: 150rpx;
				height: 168rpx;
				margin-right: 60rpx;
				padding-top: 18rpx;
				box-sizing: border-box;

				.item_cover {
					width: 150rpx;
					height: 150rpx;
					padding: 5rpx;
					box-sizing: border-box;
					background: url(#{$baseImageUrl}/bg/372_372.png) no-repeat;
					background-size: 100% 100%;
					position: relative;

					image {
						width: 100%;
						height: 100%;
					}

					.checkbox {
						width: 52rpx;
						height: 52rpx;
						position: absolute;
						top: -18rpx;
						right: -13rpx;
						z-index: 2;
					}
				}

				&:last-child {
					margin: 0;
				}
			}
		}

		.btn {
			width: 204rpx;
			height: 74rpx;
			line-height: 74rpx;
			text-align: center;
			color: #FFFFFF;
			font-size: 30rpx;
			font-weight: 400;
			font-family: SimSun;
			background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
			background-size: 100% 100%;
		}
	}
</style>